package cn.tedu.weibo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
/**
 * 利用AOP为所有Controller的功能记录日志
 */
public class LogAspect {
    @Before("execution(public cn.tedu.weibo.result.JSonResult cn.tedu.weibo.controller.*.*(..))")
    public void before(JoinPoint point){
         String className=point.getTarget().getClass().getSimpleName();
         String methodName=point.getSignature().getName();
         log.info(className+"."+methodName+"()"+"开始执行");
    }

    @AfterReturning("execution(public cn.tedu.weibo.result.JSonResult cn.tedu.weibo.controller.*.*(..))")
    public void after(JoinPoint point){
        String className=point.getTarget().getClass().getSimpleName();
        String methodName=point.getSignature().getName();
        log.info(className+"."+methodName+"()"+"执行完毕");
    }

    @AfterThrowing(
            value = "execution(public cn.tedu.weibo.result.JSonResult cn.tedu.weibo.controller.*.*(..))",
            throwing = "e"
    )
    public void throwing(JoinPoint point,Exception e){;
        log.error(e.getMessage(),e);

    }

}
